package com.jqkb;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import android.webkit.WebSettings;
import android.webkit.WebView;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jqkb.service.NewMessageService;
import com.jqkb.service.bean.User;
import com.jqkb.utils.Constant;
import com.jqkb.utils.PermisionUtils;

public class TheApplication extends Application {


    private static SharedPreferences sharedPreferences;
    private static User currentUser;
    private static String userAgentString = "";
    private static String deviceId = "";
    public static TheApplication instance;

    public static User getCurrentUser() {
        return currentUser;
    }


    public static void setCurrentUser(User currentUser) {
        TheApplication.currentUser = currentUser;
    }

    public static String getDeviceId() {
        return deviceId;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Log.i(NewMessageService.TAG, "App启动");
        this.sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        String userJson = this.sharedPreferences.getString("user", null);
        if (userJson != null) {
            try {
                JsonNode allData = objectMapper.readTree(userJson);
                currentUser = objectMapper.convertValue(allData.path("data"), User.class);
                //有用户信息则开启服务
                Intent intent = new Intent(this, NewMessageService.class);
                startService(intent);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //获取UserAgent信息
        WebView webView = new WebView(this);
        WebSettings settings = webView.getSettings();
        userAgentString = settings.getUserAgentString();
        webView.destroy();

        //获取设备编号
        deviceId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }

    public static boolean isLogin() {
        return currentUser != null;
    }

    public static String getServer() {
        return TheApplication.getSharedPreferences().getString(Constant.SERVER, "http://127.0.0.1:8800/");
    }

    public static void setServer(String ipStr) {
        sharedPreferences = TheApplication.getSharedPreferences();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Constant.SERVER, ipStr);
        editor.commit();
    }


    public static String getUserAgent() {
        return userAgentString;
    }
}
